unit =150
class MyRect:
    def __init__(self, row, col, clr):
        self.row = row
        self.col = col
        self.SetColor(clr)

    def SetColor(self, clr):
        if clr == 0:
            self.clr = (100, 0, 0)
        elif clr == 1:
            self.clr = (0, 100, 0)
        elif clr == -1:
            self.clr = (50, 50, 50)

    def Draw(self):
        rc = Rect((self.col * unit, self.row * unit), (unit, unit))
        screen.draw.filled_rect(rc, self.clr)

BACK_COLOR = (0, 0, 0)

rc_list = []

WIDTH = 750
HEIGHT = 750

blink = 0

board = [
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0]
]
for row in board:
    print(1 in row)


class Cursor:
    def __init__(self, r, c):
        self.cells = {}
        self.update(r, c)

    def update(self, r, c):
        self.cells['mid'] = [r, c]

        # up
        if r - 1 >= 0:
            self.cells['up'] = [r - 1, c]
        else:
            self.cells['up'] = None

        # down
        if r + 1 < 5:
            self.cells['down'] = [r + 1, c]
        else:
            self.cells['down'] = None

        # left
        if c - 1 >= 0:
            self.cells['left'] = [r, c - 1]
        else:
            self.cells['left'] = None

        # right
        if c + 1 < 5:
            self.cells['right'] = [r, c + 1]
        else:
            self.cells['right'] = None

cur = Cursor(2, 2)

rc_list = []

def IsVectory():
    global board
    for row in board:
        if 1 in row:
            return False

    return True

def confirm():
    global cur
    update_board(cur)

    if IsVectory() == True:
        print("Congatulation!")

# vertical move
def R_move():
    mid_row = cur.cells['mid'][0]
    mid_col = cur.cells['mid'][1] + 1
    if mid_col >= 5:
        mid_col = 0
    cur.update(mid_row, mid_col)

def L_move():
    mid_row = cur.cells['mid'][0]
    mid_col = cur.cells['mid'][1] - 1
    if mid_col < 0:
        mid_col = 4
    cur.update(mid_row, mid_col)

# horizontal move
def U_move():
    mid_row = cur.cells['mid'][0] - 1
    mid_col = cur.cells['mid'][1]
    if mid_row < 0:
        mid_row = 4
    cur.update(mid_row, mid_col)

def D_move():
    mid_row = cur.cells['mid'][0] + 1
    mid_col = cur.cells['mid'][1]
    if mid_row >= 5:
        mid_row = 0
    cur.update(mid_row, mid_col)

def flip_cell(r, c):
    global board
    board[r][c] = not board[r][c]

def update_board(cur):
    for v in cur.cells.values():
        if v == None:
            continue

        flip_cell(v[0], v[1])

def on_key_down(key):
    if key == keys.ESCAPE:
        quit()
    elif key == keys.W:
        U_move()
    elif key == keys.A:
        L_move()
    elif key == keys.S:
        D_move()
    elif key == keys.D:
        R_move()
    elif key == keys.SPACE:
        confirm()

def is_rc_exist(r, c):
    global rc_list
    for rc in rc_list:
        if rc.row == r and rc.col == c:
                return rc
    return None

def update():
    global rc_list, cur
    rc_list = []
    for r in range(5):
        for c in range(5):
            rc = MyRect(r, c, board[r][c])
            rc_list.append(rc)

    for v in cur.cells.values():
        if v == None:
            continue

        rc = is_rc_exist(v[0], v[1])
        if rc == None:
            rc = MyRect(v[0], v[1], -1)
        else:
            rc.SetColor(-1)

        rc_list.append(rc)

def draw():
    screen.clear()
    screen.fill(BACK_COLOR)
    for rc in rc_list:
        rc.Draw()
